/**
 * Chargement de la forêt des flux
 */
package ch.ifage.business.service.mock;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import ch.ifage.business.dom.Activity;
import ch.ifage.business.dom.FirstStep;
import ch.ifage.business.dom.Status;
import ch.ifage.business.dom.Step;
import ch.ifage.business.dom.StepIdentity;
import ch.ifage.business.dom.StepProperties;
import ch.ifage.business.service.Workflow;

/**
 * @author Christian
 * 
 */
public final class AnalyseFluxData {

	/** The logger for the class. */
	// @Inject
	// private transient Logger logger;

	/**
	 * 
	 */
	public AnalyseFluxData() {
		super();
	}

	/**
	 * Caractéristique du mock Nombre de flux au total.
	 */
	private Integer nbWorkflows = 9;
	/**
	 * Longueur maximale d'un flux.
	 */
	private Integer depthMax = 9;
	/**
	 * Nombre maximal de fils d'une étape.
	 */
	private Integer widthMax = 3;

	/**
	 * id unique.
	 */
	private Integer uid = 0;

	/**
	 * @return workflows
	 * @see ch.ifage.business.service.mock.WorkflowsImplMock#getList()
	 */
	public List<Workflow> getList() {
		// logger.info("Worflows built by mock.");
		/**
		 * Les workflows
		 */
		List<Workflow> list = new ArrayList<Workflow>();

		/**
		 * numéro du flux courant.
		 */
		Integer curWorkflow = 1;

		while (curWorkflow <= nbWorkflows) {
			GregorianCalendar now = new GregorianCalendar();
			now.add(GregorianCalendar.SECOND, curWorkflow);
			Workflow wf = new Workflow(getFirstStep(depthMax, now, widthMax));
			list.add(wf);
			curWorkflow += 1;
		}
		return list;
	}

	/**
	 * @return step. Step au hasard.
	 * @param depth
	 *            profondeur max des flux
	 * @param start
	 *            début de l'étape
	 * @param father
	 *            père du step
	 * @param widthMax
	 *            nombre maximum de fils
	 */
	public Step getStep(final Integer depth, final GregorianCalendar start, final Step father, final Integer widthMax) {
		uid += 1;
		StepIdentity si = new StepIdentity(uid, "serverName", "instanceName");
		Random generator = new Random();
		GregorianCalendar end = (GregorianCalendar) start.clone();
		end.add(GregorianCalendar.SECOND, generator.nextInt(100));
		Activity activity = Activity.values()[generator.nextInt(Activity.values().length)];
		DummyLog dummyLog = new DummyLog(uid, activity, 100, 100);
		StepProperties sp = new StepProperties("filename", start, end, activity, "parameter",
				Status.values()[generator.nextInt(Status.values().length)], dummyLog.getLog());
		Integer curDepth = depth - 1;
		Step s = new Step(si, sp, father);
		if (curDepth >= 1) {
			Integer nbChild = generator.nextInt(widthMax);
			while (nbChild > 0) {
				getStep(curDepth, end, s, widthMax);
				nbChild -= 1;
			}
		}
		return s;
	}

	/**
	 * @return firstStep. Step au hasard.
	 * @param depth
	 *            profondeur max des flux
	 * @param start
	 *            début de l'étape
	 * @param widthMax
	 *            nombre maximum de fils
	 */
	private FirstStep getFirstStep(final Integer depth, final GregorianCalendar start, final Integer widthMax) {
		uid += 1;
		StepIdentity si = new StepIdentity(uid, "serverName", "instanceName");
		Random generator = new Random();
		GregorianCalendar end = (GregorianCalendar) start.clone();
		end.add(GregorianCalendar.SECOND, generator.nextInt(100));
		Activity activity = Activity.values()[generator.nextInt(Activity.values().length)];
		DummyLog dummyLog = new DummyLog(uid, activity, 100, 100);
		StepProperties sp = new StepProperties("filename", start, end, activity, "parameter",
				Status.values()[generator.nextInt(Status.values().length)], dummyLog.getLog());
		Integer curDepth = depth - 1;
		FirstStep fs = new FirstStep(si, sp);
		if (curDepth >= 1) {
			Integer nbChild = generator.nextInt(widthMax);
			while (nbChild > 0) {
				getStep(curDepth, end, fs, widthMax);
				nbChild -= 1;
			}
		}
		return fs;
	}

}
